/**
 * 添加比赛模型
 *
 */

module.exports = class extends think.Model
{
    async addCPData(cpData)
    {
        return await this.model('competition').add(cpData);
    }

    async addSteps(stepsData)
    {
        return await this.model('competitionsteps').addMany(stepsData);
    }

    async addAwardsItems(awardsItemData)
    {
        return await this.model('stepsaward').addMany(awardsItemData);
    }

    async addJudger(judgerData)
    {
        return await this.model('competitionstepstojudger').addMany(judgerData);
    }

    async addCPDataSql(cpData)
    {
        return await this.model('competition').buildSql(cpData);
    }

    async addStepsSql(stepsData)
    {
        return await this.model('competitionsteps').addMany(stepsData);
    }

    async addAwardsItemsSql(awardsItemData)
    {
        return await this.model('stepsaward').addMany(awardsItemData);
    }

    async addJudgerSql(judgerData)
    {
        return await this.model('competitionstepstojudger').addMany(judgerData);
    }


    async saveCompeititonData(cpData, cpstepsData, cpawarditemData, cpjudgerData, hasAwardItems = true)
    {
        try
        {
            await this.startTrans();
            let cpModelInstance = this.model('competition').db(this.db());
            let cpStepsInstance = this.model('competitionsteps').db(this.db());
            let cpAwardItemInstance = this.model('stepsaward').db(this.db());
            let cpJudgerInstance = this.model('competitionstepstojudger').db(this.db());


            await cpModelInstance.add(cpData);  //添加比赛
            await cpStepsInstance.addMany(cpstepsData);
            await cpJudgerInstance.addMany(cpjudgerData);
            if (hasAwardItems === true) await cpAwardItemInstance.addMany(cpawarditemData);
            await this.commit();
            return {
                isErr:false,
                errNo: 0,
                errMsg: '添加成功'
            }
        } catch (e)
        {
            await  this.rollback();
            return {
                isErr:true,
                errNo: 52041,
                errMsg: e.message
            }
        }

    }

};
